import { CopyControllerBase } from "./CopyControllerBase";
import { DataEvent } from "../../../events/DataEvent";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { SPlayerGuildInfo } from "../../../../../Message/Game/SPlayerGuildInfo";
import { EGuildPosition } from "../../../../../Message/Game/EGuildPosition";
import { CopyUtil } from "../CopyUtil";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { GameProxy } from "../../../mvc/GameProxy";
import { EWuXingAttribute } from "../../../../../Message/Public/EWuXingAttribute";
import { ECopyType } from "../../../../../Message/Public/ECopyType";
import { SGuildStruggleInfo } from "../../../../../Message/Public/SGuildStruggleInfo";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { CopyConfig } from "../../../resource/CopyConfig";
import { GuildCopyView } from "../guildCopy/GuildCopyView";
import { GameMapUtil } from "../../../scene/map/GameMapUtil";
import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../../../scene/events/PlayerEvent";
import { SGuildStruggleProcess } from "../../../../../Message/Public/SGuildStruggleProcess";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SSeqAttributeUpdate } from "../../../../../Message/Public/SSeqAttributeUpdate";
import { EEntityAttribute } from "../../../../../Message/Public/EEntityAttribute";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { SPassTo } from "../../../../../Message/Public/SPassTo";
import { EPassType } from "../../../../../Message/Game/EPassType";
import { IView } from "../../../../mvc/interfaces/IView";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { TaskTargetInfo } from "../../../model/TaskTargetInfo";
type int = number;
//class GuildCopyController
    
    export  class GuildCopyController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        public /* override */ stageResize(): void
        {
            this.guildCopyView.stageResize();
            return;
        }

        /* internal  */onOpenGuildCopyWindow(arg1: DataEvent): void
        {
            if (cache.guild.myGuildInfo == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41833));
                return;
            }
            var loc1=-1;
            if (typeof arg1.data === "int") 
            {
                loc1 = arg1.data as int;
            }
            this.guildCopyView.showOpenCopyWin(loc1);
            return;
        }

        /* internal  */onHideOpenCopyWindow(arg1: DataEvent): void
        {
            this.guildCopyView.hideOpenCopyWin();
            return;
        }

        /* internal  */onUpdateEntranceStatus(arg1): void
        {
            this.guildCopyView.updateOpenWinStatus();
            return;
        }

        /* internal  */onGetStruggleInfoRes(arg1: DataEvent): void
        {
            this.guildCopyView.updateOpenWinStatus();
            return;
        }

        /* internal  */doOpenGuildCopy(arg1: DataEvent): void
        {
            var e: DataEvent;
            var info: SPlayerGuildInfo;
            var diff: int;
            var mode: string;
            var desc: string;

            var loc1;
            diff = 0;
            mode = null;
            desc = null;
            e = arg1;
            info = cache.guild.myGuildInfo;
            if (info && (info.position == EGuildPosition._EGuildLeader || info.position == EGuildPosition._EGuildDeputyLeader)) 
            {
                diff = e.data as int;
                mode = CopyUtil.getDifficultyDef(diff);
                desc = CopyUtil.getDifficultyPlayerDef(diff);
                Alert.show(Language.getStringByParam(60426, mode, desc), Language.getString(69900), Alert.OK | Alert.CANCEL, null, (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        GameProxy.copy.openGuildCopy(diff);
                    }
                    return;
                })
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60409));
            }
            return;
        }

        /* internal  */doAddGuildWuxingAttribute(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.copy.addWuXingAttribute(new EWuXingAttribute(loc1));
            return;
        }

        /* internal  */doEnterGuildCopy(arg1: DataEvent): void
        {
            GameProxy.copy.enterGuildCopy();
            return;
        }

        /* internal  */showGuildCopyInfo(): void
        {
            if (cache.copy.isInCopyType(ECopyType._ECopyGuildStruggle)) 
            {
                this.guildCopyView.showGuildCopyInfo(copy);
                this.guildCopyView.stageResize();
            }
            return;
        }

        /* internal  */onGetGuildStruggleInfo(arg1: SGuildStruggleInfo): void
        {
            if (this._guildCopyView) 
            {
                this.guildCopyView.updateCopyInfo(arg1);
            }
            return;
        }

        /* internal  */doUpdateInfoPanel(arg1: DataEvent): void
        {
            var loc1=null;
            if (this._guildCopyView) 
            {
                loc1 = cache.copy.guildStruggleInfo;
                this.guildCopyView.updateCopyInfo(loc1);
            }
            return;
        }

        /* internal  */onShowNoticeMsg(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            MsgManager.showTaskTarget(loc1, 99999);
            Dispatcher.addEventListener(EventName.ChangeScene, this.removeNoticeMsg);
            return;
        }

        /* internal  */removeNoticeMsg(arg1): void
        {
            Dispatcher.removeEventListener(EventName.ChangeScene, this.removeNoticeMsg);
            MsgManager.removeTaskTarget();
            return;
        }

        /* internal  */onEnterLayer(arg1: SGuildStruggleInfo): void
        {
            var loc1=CopyConfig.instance.getGuildStruggleConfig(arg1.layer, arg1.difficulty);
            var loc2=cache.copy.guildStruggleProcess;
            if (loc1) 
            {
                MsgManager.showTaskTarget(loc1.broadcastEnter);
            }
            if (loc1.layer != 1) 
            {
                if (loc1.layer == 2) 
                {
                    this.guildCopyView.showGirlEnterSecondFloor();
                }
            }
            else 
            {
                flash.utils.setTimeout(this.beginTargetPoint, 1000);
            }
            return;
        }

        public get guildCopyView(): GuildCopyView
        {
            if (!this._guildCopyView) 
            {
                this.initView();
            }
            return this._guildCopyView;
        }

        /* internal  */beginTargetPoint(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=false;
            if (GameMapUtil.curMapState.isCopyMap && cache.copy.inWhichLayerOfGuildCopy() == 1) 
            {
                loc1 = cache.copy.guildStruggleInfo;
                loc2 = cache.copy.guildStruggleProcess;
                loc3 = CopyConfig.instance.getGuildStruggleConfig(loc1.layer, loc1.difficulty);
                loc4 = CopyUtil.isGuildCopyWuxingBossEnd(loc1, loc2, loc3);
                if (loc1 && !loc1.isLayerEnd && !loc4) 
                {
                    this._target = cache.scene.getNearlyGuildCopyWuxingNpc();
                    if (this._target) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GuideDirBegin, this._target));
                        RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onPlayerWalkTargetHandler);
                        Dispatcher.addEventListener(EventName.CopyGuildAddWuxingAttSuccess, this.endTargetPoint);
                        Dispatcher.addEventListener(EventName.ChangeScene, this.endTargetPoint);
                    }
                }
            }
            return;
        }

        /* internal  */endTargetPoint(arg1): void
        {
            this._target = null;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuideDirEnd));
            RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onPlayerWalkTargetHandler);
            Dispatcher.removeEventListener(EventName.CopyGuildAddWuxingAttSuccess, this.endTargetPoint);
            Dispatcher.removeEventListener(EventName.ChangeScene, this.endTargetPoint);
            return;
        }

        /* internal  */onPlayerWalkTargetHandler(arg1): void
        {
            var loc1=cache.scene.getNearlyGuildCopyWuxingNpc();
            if (this._target && !(this._target.id == loc1.id)) 
            {
                this._target = loc1;
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuideDirBegin, this._target));
            }
            return;
        }

        /* internal  */onGetGuildStruggleProcess(arg1: SGuildStruggleProcess): void
        {
            if (this._guildCopyView) 
            {
                this.guildCopyView.updateProcess(arg1);
            }
            return;
        }

        /* internal  */onGuildStruggleOpen(arg1): void
        {
            cache.copy.isGuildCopyOpened = true;
            cache.copy.guildCopyExp = 0;
            cache.copy.guildCopyContribution = 0;
            this.guildCopyView.showOpenHintIcon();
            GameProxy.copy.getGuildStruggleInfo();
            return;
        }

        /* internal  */onGuildStruggleClose(arg1): void
        {
            cache.copy.isGuildCopyOpened = false;
            this.guildCopyView.updateOpenWinStatus();
            this.guildCopyView.hideOpenHintIcon();
            return;
        }

        /* internal  */onShowStruggleResult(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSeqAttributeUpdate;
            var loc2=this.getValue(EEntityAttribute._EAttributeExperienceAdd, loc1.updates);
            var loc3=this.getValue(EEntityAttribute._EAttributeContributionAdd, loc1.updates);
            Alert.show(Language.getStringByParam(60432, loc2, loc3), Language.getString(60400));
            return;
        }

        /* internal  */getValue(arg1: int, arg2: Array<any>): int
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg2;
            for(loc1 of loc3) 
            {
                if (loc1.attribute.value() != arg1) 
                {
                    continue;
                }
                return loc1.value;
            }
            return 0;
        }

        /* internal  */onUpdateExp(arg1: DataEvent): void
        {
            if (this._guildCopyView) 
            {
                this.guildCopyView.updateExp(arg1.data as int);
            }
            return;
        }

        /* internal  */onUpdateContribute(arg1: DataEvent): void
        {
            if (this._guildCopyView) 
            {
                this.guildCopyView.updateContribute(arg1.data as int);
            }
            return;
        }

        /* internal  */onLeaveGuild(arg1): void
        {
            if (this._guildCopyView) 
            {
                this._guildCopyView.hideOpenHintIcon();
            }
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            flash.utils.setTimeout(this.showGuildCopyInfo, 2000);
            this.guildCopyView.hideOpenCopyWin();
            return;
        }

        public /* override */ leaveCopy(): void
        {
            this.guildCopyView.hideGuildCopyInfo();
            cache.copy.guildStruggleInfo = null;
            cache.copy.guildStruggleProcess = null;
            return;
        }

        public /* override */ updateCopyProcess(): void
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc1=cache.copy.process;
            if (cache.copy.inWhichLayerOfGuildCopy() == 1 && loc1 == 2 || cache.copy.inWhichLayerOfGuildCopy() == 2 && loc1 == 3) 
            {
                loc2 = cache.scene.getNearlyPassPoint(false);
                if (loc2) 
                {
                    loc3 = loc2.point.x;
                    loc4 = loc2.point.y;
                    RolePlayer.instance.setTargetTilePoint(loc3, loc4);
                    Dispatcher.addEventListener(EventName.ChangeScene, this.onCopyChangeSceneHandler);
                }
                if (cache.copy.inWhichLayerOfGuildCopy() == 2) 
                {
                    Alert.showCloseBtn = true;
                    Alert.buttonWidth = 75;
                    Alert.okLabel = Language.getString(60431);
                    Alert.show(Language.getString(60430), Language.getString(69900), Alert.OK, null, this.onEnterThirdFloorHandler);
                }
            }
            return;
        }

        /* internal  */onEnterThirdFloorHandler(arg1: int): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1 == Alert.OK) 
            {
                loc1 = cache.scene.getNearlyPassPoint(false);
                loc2 = loc1.passTo[0] as SPassTo;
                if (loc2) 
                {
                    GameProxy.sceneProxy.pass(EPassType.convert(EPassType._EPassTypeByPassPoint), loc1.passPointId, loc2.passToId);
                }
            }
            return;
        }

        /* internal  */onCopyChangeSceneHandler(arg1: DataEvent): void
        {
            RolePlayer.instance.removeTargetTilePoint();
            Dispatcher.removeEventListener(EventName.ChangeScene, this.onCopyChangeSceneHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._guildCopyView) 
            {
                this._guildCopyView = new GuildCopyView();
            }
            return this._guildCopyView;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.GuildOpenCopyWindow, this.onOpenGuildCopyWindow);
            Dispatcher.addEventListener(EventName.GuildHideOpenCopyWindow, this.onHideOpenCopyWindow);
            Dispatcher.addEventListener(EventName.CopyUpdateGuildCopyEntranceStatus, this.onUpdateEntranceStatus);
            Dispatcher.addEventListener(EventName.CopyOpenGuildCopy, this.doOpenGuildCopy);
            Dispatcher.addEventListener(EventName.CopyEnterGuildCopy, this.doEnterGuildCopy);
            Dispatcher.addEventListener(EventName.CopyGuildAddWuxingAtt, this.doAddGuildWuxingAttribute);
            Dispatcher.addEventListener(EventName.GuildCopyGetStruggleInfoRes, this.onGetStruggleInfoRes);
            Dispatcher.addEventListener(EventName.GuildCopyUpdateInfoPanel, this.doUpdateInfoPanel);
            Dispatcher.addEventListener(EventName.GuildCopyShowNoticeMsg, this.onShowNoticeMsg);
            Dispatcher.addEventListener(EventName.GuildCopyUpdateExp, this.onUpdateExp);
            Dispatcher.addEventListener(EventName.GuildCopyUpdateContribute, this.onUpdateContribute);
            NetDispatcher.addCmdListener(ServerCommand.GuildStruggleInfoChange, this.onGetGuildStruggleInfo);
            NetDispatcher.addCmdListener(ServerCommand.GuildStruggleProcessChange, this.onGetGuildStruggleProcess);
            NetDispatcher.addCmdListener(ServerCommand.GuildStruggleEnterLayer, this.onEnterLayer);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildStruggleOpen, this.onGuildStruggleOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildStruggleClose, this.onGuildStruggleClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildStrugglePersonalResult, this.onShowStruggleResult);
            NetDispatcher.addCmdListener(ServerCommand.GuildLeaveGuild, this.onLeaveGuild);
            return;
        }

        /* internal */ /* var */_guildCopyView: GuildCopyView;

        /* internal */ /* var */_target: TaskTargetInfo;
    }
